package com.anglireports.report.data.common.angli;

import com.alibaba.fastjson.JSONObject;
import com.anglireports.report.response.anliResult.*;
import com.anglireports.report.response.util.Constant;
import com.anglireports.report.response.util.HttpSendRequest;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 昂立接口数据
 * @author lvjilei
 * @ceateTime 2019/4/2.
 */
public class AngliCommonData {

    /**
     * 获取校区
     * @param date 格式 yyyy-MM-dd
     * @returnOrgResult
     */
    public static OrgResult getOrgList(String date){
        OrgResult re = new OrgResult();
        Map<String, Object> mapParams = new HashMap<String, Object>();
        mapParams.put("date", date);
        JSONObject result = HttpSendRequest.doGet(Constant.ANGLI_SCHOOL_API, mapParams);
        re  = JSONObject.toJavaObject(result, OrgResult.class);
        return re;
    }

    /**
     * 获取校区集合
     * @param date 格式 yyyy-MM-dd
     * @return
     */
    public static Map<String, OrgModel> getOrgModelMap(String date ){

        OrgResult orgResult = getOrgList(date);
        if(orgResult.getCode() == 0 &&  orgResult.getPayload() != null
                && orgResult.getPayload().getList().size() > 0){

            Map<String, OrgModel> orgModelMap = new HashMap<>();
            for(OrgModel orgModel : orgResult.getPayload().getList()){
                orgModelMap.put(orgModel.getOrg_code(), orgModel);
            }
            return orgModelMap;
       }
        return null;
    }

    /**
     * 获取校区班级
     * @param org_code 校区编码
     * @return
     */
    public static ClassResult getClassList(String org_code){
        ClassResult re = new ClassResult();
        Map<String, Object> mapParams = new HashMap<String, Object>();
        mapParams.put("org_code", org_code);
        JSONObject result = HttpSendRequest.doGet(Constant.ANGLI_CALSS_API, mapParams);
        re  = JSONObject.toJavaObject(result, ClassResult.class);
        return re;
    }


    /**
     * 获取校区班级集合
     * @param org_code 校区编码
     * @return
     */
    public static Map<String, ClassModel> getClassModelMap(String org_code){
        ClassResult classResult = getClassList(org_code);
        if(classResult.getCode() == 0 &&  classResult.getPayload() != null
                && classResult.getPayload().getList().size() > 0){

            Map<String, ClassModel> classModelMap = new HashMap<>();
            for(ClassModel classModel : classResult.getPayload().getList()){
                classModelMap.put(classModel.getClass_id()+"", classModel);
            }
            return classModelMap;
        }
        return null;
    }


    /**
     * 获取学生和教师
     * @param class_id 班级id
     * @return
     */
    public static StuAndTeaResult getStuAndTeaList(int class_id){
        StuAndTeaResult re = new StuAndTeaResult();
        Map<String, Object> mapParams = new HashMap<String, Object>();
        mapParams.put("class_id", class_id);
        JSONObject result = HttpSendRequest.doGet(Constant.ANGLI_STUANDTEA, mapParams);
        re  = JSONObject.toJavaObject(result, StuAndTeaResult.class);
        return re;
    }


    /**
     * 获取学生和教师集合
     * @param class_id 班级id
     * @return
     */
    public static Map<String, Map<String,Object>> getStuAndTeaMap(int class_id){
        StuAndTeaResult stuAndTeaResult = getStuAndTeaList(class_id);
        if(stuAndTeaResult.getCode() == 0 &&  stuAndTeaResult.getPayload() != null){
            Map<String, Map<String, Object>> reMap = new HashMap<>();
            if(stuAndTeaResult.getPayload().getStudentList().size() > 0){
                Map<String, Object> studentModelMap = new HashMap<>();
                for(StudentModel studentModel : stuAndTeaResult.getPayload().getStudentList()){
                    studentModelMap.put(studentModel.getMobile(), studentModel);
                }
                reMap.put("studentModelMap", studentModelMap);
            }

            if(stuAndTeaResult.getPayload().getTeacherList().size() > 0){
                Map<String, Object> teachModelMap = new HashMap<>();
                for(TeacherModel teacherModel : stuAndTeaResult.getPayload().getTeacherList()){
                    teachModelMap.put(teacherModel.getMobile(), teacherModel);
                }
                reMap.put("teachModelMap", teachModelMap);
            }
            return reMap;
        }
        return null;
    }


    /**
     * 获取学生集合
     * @param class_id 班级id
     * @return
     */
    public static Map<String, StudentModel> getStuMap(int class_id){
        Map<String, Map<String,Object>> stuAndTeaMap = getStuAndTeaMap(class_id);
        if(stuAndTeaMap != null && stuAndTeaMap.get("studentModelMap") != null ){
            Map<String,Object> studentMap = stuAndTeaMap.get("studentModelMap");

            Map<String,StudentModel> rs = new HashMap<>();
            Iterator<String> sets = studentMap.keySet().iterator();
            while (sets.hasNext()){
                StudentModel studentModel = (StudentModel) studentMap.get(sets.next());
                rs.put(studentModel.getMobile() ,studentModel);

            }
            return rs;
        }
        return null;
    }


    /*public static void main(String[] args){
        AngliCommonData.getStuAndTeaList(73);
    }*/

}
